home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 022 / lemacs / basic.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  11KB  |  380 lines

  1. /*
  2.  * The routines in this file move the cursor around on the screen. They
  3.  * compute a new value for the cursor, then adjust ".". The display code
  4.  * always updates the cursor location, so only moves between lines, or
  5.  * functions that adjust the top line in the window and invalidate the
  6.  * framing, are hard.
  7.  */
  8. #include        <stdio.h>
  9. #include    "estruct.h"
  10. #include        "edef.h"
  11.  
  12. /*
  13.  * Move the cursor to the
  14.  * beginning of the current line.
  15.  * Trivial.
  16.  */
  17. gotobol(f, n)
  18. {
  19.         curwp->w_doto  = 0;
  20.         return (TRUE);
  21. }
  22.  
  23. /*
  24.  * Move the cursor backwards by "n" characters. If "n" is less than zero call
  25.  * "forwchar" to actually do the move. Otherwise compute the new cursor
  26.  * location. Error if you try and move out of the buffer. Set the flag if the
  27.  * line pointer for dot changes.
  28.  */
  29. backchar(f, n)
  30. register int    n;
  31. {
  32.         register LINE   *lp;
  33.  
  34.         if (n < 0)
  35.                 return (forwchar(f, -n));
  36.         while (n--) {
  37.                 if (curwp->w_doto == 0) {
  38.                         if ((lp=lback(curwp->w_dotp)) == curbp->b_linep)
  39.                                 return (FALSE);
  40.                         curwp->w_dotp  = lp;
  41.                         curwp->w_doto  = llength(lp);
  42.                         curwp->w_flag |= WFMOVE;
  43.                 } else
  44.                         curwp->w_doto--;
  45.         }
  46.         return (TRUE);
  47. }
  48.  
  49. /*
  50.  * Move the cursor to the end of the current line. Trivial. No errors.
  51.  */
  52. gotoeol(f, n)
  53. {
  54.         curwp->w_doto  = llength(curwp->w_dotp);
  55.         return (TRUE);
  56. }
  57.  
  58. /*
  59.  * Move the cursor forwwards by "n" characters. If "n" is less than zero call
  60.  * "backchar" to actually do the move. Otherwise compute the new cursor
  61.  * location, and move ".". Error if you try and move off the end of the
  62.  * buffer. Set the flag if the line pointer for dot changes.
  63.  */
  64. forwchar(f, n)
  65. register int    n;
  66. {
  67.         if (n < 0)
  68.                 return (backchar(f, -n));
  69.         while (n--) {
  70.                 if (curwp->w_doto == llength(curwp->w_dotp)) {
  71.                         if (curwp->w_dotp == curbp->b_linep)
  72.                                 return (FALSE);
  73.                         curwp->w_dotp  = lforw(curwp->w_dotp);
  74.                         curwp->w_doto  = 0;
  75.                         curwp->w_flag |= WFMOVE;
  76.                 } else
  77.                         curwp->w_doto++;
  78.         }
  79.         return (TRUE);
  80. }
  81.  
  82. gotoline(f, n)        /* move to a particular line.
  83.                argument (n) must be a positive integer for
  84.                this to actually do anything        */
  85.  
  86. {
  87.     if (n < 1)        /* if a bogus argument...then leave */
  88.         return(FALSE);
  89.  
  90.     /* first, we go to the start of the buffer */
  91.         curwp->w_dotp  = lforw(curbp->b_linep);
  92.         curwp->w_doto  = 0;
  93.     return(forwline(f, n-1));
  94. }
  95.  
  96. /*
  97.  * Goto the beginning of the buffer. Massive adjustment of dot. This is
  98.  * considered to be hard motion; it really isn't if the original value of dot
  99.  * is the same as the new value of dot. Normally bound to "M-<".
  100.  */
  101. gotobob(f, n)
  102. {
  103.         curwp->w_dotp  = lforw(curbp->b_linep);
  104.         curwp->w_doto  = 0;
  105.         curwp->w_flag |= WFHARD;
  106.         return (TRUE);
  107. }
  108.  
  109. /*
  110.  * Move to the end of the buffer. Dot is always put at the end of the file
  111.  * (ZJ). The standard screen code does most of the hard parts of update.
  112.  * Bound to "M->".
  113.  */
  114. gotoeob(f, n)
  115. {
  116.         curwp->w_dotp  = curbp->b_linep;
  117.         curwp->w_doto  = 0;
  118.         curwp->w_flag |= WFHARD;
  119.         return (TRUE);
  120. }
  121.  
  122. /*
  123.  * Move forward by full lines. If the number of lines to move is less than
  124.  * zero, call the backward line function to actually do it. The last command
  125.  * controls how the goal column is set. Bound to "C-N". No errors are
  126.  * possible.
  127.  */
  128. forwline(f, n)
  129. {
  130.         register LINE   *dlp;
  131.  
  132.         if (n < 0)
  133.                 return (backline(f, -n));
  134.         if ((lastflag&CFCPCN) == 0)             /* Reset goal if last   */
  135.                 curgoal = getccol(FALSE);       /* not C-P or C-N       */
  136.         thisflag |= CFCPCN;
  137.         dlp = curwp->w_dotp;
  138.         while (n-- && dlp!=curbp->b_linep)
  139.                 dlp = lforw(dlp);
  140.         curwp->w_dotp  = dlp;
  141.         curwp->w_doto  = getgoal(dlp);
  142.         curwp->w_flag |= WFMOVE;
  143.         return (TRUE);
  144. }
  145.  
  146. /*
  147.  * This function is like "forwline", but goes backwards. The scheme is exactly
  148.  * the same. Check for arguments that are less than zero and call your
  149.  * alternate. Figure out the new line and call "movedot" to perform the
  150.  * motion. No errors are possible. Bound to "C-P".
  151.  */
  152. backline(f, n)
  153. {
  154.         register LINE   *dlp;
  155.  
  156.         if (n < 0)
  157.                 return (forwline(f, -n));
  158.         if ((lastflag&CFCPCN) == 0)             /* Reset goal if the    */
  159.                 curgoal = getccol(FALSE);       /* last isn't C-P, C-N  */
  160.         thisflag |= CFCPCN;
  161.         dlp = curwp->w_dotp;
  162.         while (n-- && lback(dlp)!=curbp->b_linep)
  163.                 dlp = lback(dlp);
  164.         curwp->w_dotp  = dlp;
  165.         curwp->w_doto  = getgoal(dlp);
  166.         curwp->w_flag |= WFMOVE;
  167.         return (TRUE);
  168. }
  169.  
  170. gotobop(f, n)    /* go back to the begining of the current paragraph
  171.            here we look for a <NL><NL> or <NL><TAB> or <NL><SPACE>
  172.            combination to delimit the begining of a paragraph    */
  173.  
  174. int f, n;    /* default Flag & Numeric argument */
  175.  
  176. {
  177.     register int suc;    /* success of last backchar */
  178.  
  179.     if (n < 0)    /* the other way...*/
  180.         return(gotoeop(f, -n));
  181.  
  182.     while (n-- > 0) {    /* for each one asked for */
  183.  
  184.         /* first scan back until we are in a word */
  185.         suc = backchar(FALSE, 1);
  186.         while (!inword() && suc)
  187.             suc = backchar(FALSE, 1);
  188.         curwp->w_doto = 0;    /* and go to the B-O-Line */
  189.  
  190.         /* and scan back until we hit a <NL><NL> or <NL><TAB>
  191.            or a <NL><SPACE>                    */
  192.         while (lback(curwp->w_dotp) != curbp->b_linep)
  193.             if (llength(curwp->w_dotp) != 0 &&
  194.                 lgetc(curwp->w_dotp, curwp->w_doto) != TAB &&
  195.                 lgetc(curwp->w_dotp, curwp->w_doto) != ' ')
  196.                 curwp->w_dotp = lback(curwp->w_dotp);
  197.             else
  198.                 break;
  199.  
  200.         /* and then forward until we are in a word */
  201.         suc = forwchar(FALSE, 1);
  202.         while (suc && !inword())
  203.             suc = forwchar(FALSE, 1);
  204.     }
  205.     curwp->w_flag |= WFMOVE;    /* force screen update */
  206. }
  207.  
  208. gotoeop(f, n)    /* go forword to the end of the current paragraph
  209.            here we look for a <NL><NL> or <NL><TAB> or <NL><SPACE>
  210.            combination to delimit the begining of a paragraph    */
  211.  
  212. int f, n;    /* default Flag & Numeric argument */
  213.  
  214. {
  215.     register int suc;    /* success of last backchar */
  216.  
  217.     if (n < 0)    /* the other way...*/
  218.         return(gotobop(f, -n));
  219.  
  220.     while (n-- > 0) {    /* for each one asked for */
  221.  
  222.         /* first scan forward until we are in a word */
  223.         suc = forwchar(FALSE, 1);
  224.         while (!inword() && suc)
  225.             suc = forwchar(FALSE, 1);
  226.         curwp->w_doto = 0;    /* and go to the B-O-Line */
  227.         if (suc)    /* of next line if not at EOF */
  228.             curwp->w_dotp = lforw(curwp->w_dotp);
  229.  
  230.         /* and scan forword until we hit a <NL><NL> or <NL><TAB>
  231.            or a <NL><SPACE>                    */
  232.         while (curwp->w_dotp != curbp->b_linep) {
  233.             if (llength(curwp->w_dotp) != 0 &&
  234.                 lgetc(curwp->w_dotp, curwp->w_doto) != TAB &&
  235.                 lgetc(curwp->w_dotp, curwp->w_doto) != ' ')
  236.                 curwp->w_dotp = lforw(curwp->w_dotp);
  237.             else
  238.                 break;
  239.         }
  240.  
  241.         /* and then backward until we are in a word */
  242.         suc = backchar(FALSE, 1);
  243.         while (suc && !inword()) {
  244.             suc = backchar(FALSE, 1);
  245.         }
  246.         curwp->w_doto = llength(curwp->w_dotp);    /* and to the EOL */
  247.     }
  248.     curwp->w_flag |= WFMOVE;    /* force screen update */
  249. }
  250.  
  251. /*
  252.  * This routine, given a pointer to a LINE, and the current cursor goal
  253.  * column, return the best choice for the offset. The offset is returned.
  254.  * Used by "C-N" and "C-P".
  255.  */
  256. getgoal(dlp)
  257. register LINE   *dlp;
  258. {
  259.         register int    c;
  260.         register int    col;
  261.         register int    newcol;
  262.         register int    dbo;
  263.  
  264.         col = 0;
  265.         dbo = 0;
  266.         while (dbo != llength(dlp)) {
  267.                 c = lgetc(dlp, dbo);
  268.                 newcol = col;
  269.                 if (c == '\t')
  270.                         newcol |= 0x07;
  271.                 else if (c<0x20 || c==0x7F)
  272.                         ++newcol;
  273.                 ++newcol;
  274.                 if (newcol > curgoal)
  275.                         break;
  276.                 col = newcol;
  277.                 ++dbo;
  278.         }
  279.         return (dbo);
  280. }
  281.  
  282. /*
  283.  * Scroll forward by a specified number of lines, or by a full page if no
  284.  * argument. Bound to "C-V". The "2" in the arithmetic on the window size is
  285.  * the overlap; this value is the default overlap value in ITS EMACS. Because
  286.  * this zaps the top line in the display window, we have to do a hard update.
  287.  */
  288. forwpage(f, n)
  289. register int    n;
  290. {
  291.         register LINE   *lp;
  292.  
  293.         if (f == FALSE) {
  294.                 n = curwp->w_ntrows - 2;        /* Default scroll.      */
  295.                 if (n <= 0)                     /* Forget the overlap   */
  296.                         n = 1;                  /* if tiny window.      */
  297.         } else if (n < 0)
  298.                 return (backpage(f, -n));
  299. #if     CVMVAS
  300.         else                                    /* Convert from pages   */
  301.                 n *= curwp->w_ntrows;           /* to lines.            */
  302. #endif
  303.         lp = curwp->w_linep;
  304.         while (n-- && lp!=curbp->b_linep)
  305.                 lp = lforw(lp);
  306.         curwp->w_linep = lp;
  307.         curwp->w_dotp  = lp;
  308.         curwp->w_doto  = 0;
  309.         curwp->w_flag |= WFHARD;
  310.         return (TRUE);
  311. }
  312.  
  313. /*
  314.  * This command is like "forwpage", but it goes backwards. The "2", like
  315.  * above, is the overlap between the two windows. The value is from the ITS
  316.  * EMACS manual. Bound to "M-V". We do a hard update for exactly the same
  317.  * reason.
  318.  */
  319. backpage(f, n)
  320. register int    n;
  321. {
  322.         register LINE   *lp;
  323.  
  324.         if (f == FALSE) {
  325.                 n = curwp->w_ntrows - 2;        /* Default scroll.      */
  326.                 if (n <= 0)                     /* Don't blow up if the */
  327.                         n = 1;                  /* window is tiny.      */
  328.         } else if (n < 0)
  329.                 return (forwpage(f, -n));
  330. #if     CVMVAS
  331.         else                                    /* Convert from pages   */
  332.                 n *= curwp->w_ntrows;           /* to lines.            */
  333. #endif
  334.         lp = curwp->w_linep;
  335.         while (n-- && lback(lp)!=curbp->b_linep)
  336.                 lp = lback(lp);
  337.         curwp->w_linep = lp;
  338.         curwp->w_dotp  = lp;
  339.         curwp->w_doto  = 0;
  340.         curwp->w_flag |= WFHARD;
  341.         return (TRUE);
  342. }
  343.  
  344. /*
  345.  * Set the mark in the current window to the value of "." in the window. No
  346.  * errors are possible. Bound to "M-.".
  347.  */
  348. setmark(f, n)
  349. {
  350.         curwp->w_markp = curwp->w_dotp;
  351.         curwp->w_marko = curwp->w_doto;
  352.         mlwrite("[Mark set]");
  353.         return (TRUE);
  354. }
  355.  
  356. /*
  357.  * Swap the values of "." and "mark" in the current window. This is pretty
  358.  * easy, bacause all of the hard work gets done by the standard routine
  359.  * that moves the mark about. The only possible error is "no mark". Bound to
  360.  * "C-X C-X".
  361.  */
  362. swapmark(f, n)
  363. {
  364.         register LINE   *odotp;
  365.         register int    odoto;
  366.  
  367.         if (curwp->w_markp == NULL) {
  368.                 mlwrite("No mark in this window");
  369.                 return (FALSE);
  370.         }
  371.         odotp = curwp->w_dotp;
  372.         odoto = curwp->w_doto;
  373.         curwp->w_dotp  = curwp->w_markp;
  374.         curwp->w_doto  = curwp->w_marko;
  375.         curwp->w_markp = odotp;
  376.         curwp->w_marko = odoto;
  377.         curwp->w_flag |= WFMOVE;
  378.         return (TRUE);
  379. }
  380.